﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using YArchitech.LIB;
using YArchitech.MEP.Electric.Common;
using libxl;

namespace YArchitech.MEP.Electric.CableLay
{
	public static class CableLayCommon
	{
		public static string GetTemplateDataFile()
		{
			return Path.Combine(Product.DataLocation, "ElectricalData", "CableLay.xml");
		}

		public static string GetUserDataFile()
		{
			string text = Path.Combine(Product.UserDataLocation, "CableLay.xml");
			if (!File.Exists(text))
			{
				string templateDataFile = CableLayCommon.GetTemplateDataFile();
				if (!File.Exists(templateDataFile))
				{
					return string.Empty;
				}
				File.Copy(templateDataFile, text);
			}
			return text;
		}

		public static string ReadExcelCell(Sheet sheet, int row, int col)
		{
			if (sheet == null)
			{
				return string.Empty;
			}
			string text = string.Empty;
			string result;
			try
			{
				CellType cellType = sheet.cellType(row, col);
				if (cellType == CellType.CELLTYPE_NUMBER)
				{
					text = sheet.readNum(row, col).ToString();
				}
				else if (cellType == CellType.CELLTYPE_STRING)
				{
					text = sheet.readStr(row, col);
				}
				else if (cellType == CellType.CELLTYPE_BOOLEAN)
				{
					text = sheet.readBool(row, col).ToString();
				}
				result = text;
			}
			catch (System.Exception)
			{
				result = string.Empty;
			}
			return result;
		}

		public static double VolumeRatioOfSystemType(string systemType)
		{
			if (string.IsNullOrWhiteSpace(systemType))
			{
				return 0.0;
			}
			List<XmlElement> elements = new YArchitech.MEP.Electric.Common.XmlHelper(CableLayCommon.GetUserDataFile(), "ElectricData").GetElements("CableLayRule");
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			foreach (XmlElement xmlElement in elements)
			{
				string attribute = xmlElement.GetAttribute("Name");
				string attribute2 = xmlElement.GetAttribute("Value");
				dictionary.Add(attribute, attribute2);
			}
			string text = dictionary[systemType];
			if (!string.IsNullOrWhiteSpace(text))
			{
				return double.Parse(text);
			}
			return 0.0;
		}

		public static bool IsApplyCableLayRule()
		{
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(CableLayCommon.GetUserDataFile(), "ElectricData");
			xmlHelper.GetElements("CableLayRule");
			return Convert.ToBoolean(xmlHelper.GetAttributeEx("IsChecked", "false"));
		}

		public static char[] m_sp = new char[]
		{
			','
		};
	}
}
